﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/HT_Layout.cshtml";
}

<div class="box-body">
    <div class="btn btn-success" id="Add">添加口味</div>
    <table class="display table table-hover table-bordered datatable" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>编号</th>
                <th>添加口味</th>
                <th>操作</th>
            </tr>
        </thead>
    </table>
</div>

@section  myScript{
    <script>
        var el = $(".datatable");
        var url = serverPath + "HT_Taste/GetAll";
        var menthod = "post";
        var colunm = [
            { data: 'Id' },
            { data: 'TasteName' },
            { data: 'cc' }
        ];
        //初始化dataatable
        var datatable = DataTableInit(el, url, menthod, colunm);

        //添加
        $("#Add").click(function () {
            $("#myModal h3").text("添加口味");
            //获取添加视图，并将其显示在modal上
            $.get(serverPath + "HT_Taste/Add", null, function (data) {
                $("#myModal .modal-body").html(data);
            })

            $("#myModal #btnSubmit").click(function () {
                var p = {
                    TasteName: $("#TasteName").val(),
                };
                console.log(p);
                $.post(serverPath + "HT_Taste/Add", p, function (data) {
                    if (data.Code == 200) {
                        layer.msg(data.Message);
                        $("#myModal").modal("hide");
                        datatable.ajax.reload();
                    }
                })
            })
            $("#myModal").modal("show");
        })

        //删除
        function Delete(a) {
            var id = $(a).attr("data-id");
            $.post(serverPath + "HT_Taste/Delete", { id }, function (data) {
                if (data.Code == 200) {
                    layer.msg(data.Message);
                    datatable.ajax.reload();
                } else {
                    layer.msg("删除失败");
                }
            })
        }

        //更新
        function Edit(a) {
            var id = $(a).attr("data-id");
            $("#myModal h3").text("修改层数");
            $.get(serverPath + "HT_Taste/Edit", { id }, function (data) {
                $("#myModal .modal-body").html(data);
                $("#myModal").modal("show");

                $("#btnSubmit").on("click", function () {
                    var p = {
                        Id: id,
                        TasteName: $("#TasteName").val()
                    }
                    $.post(serverPath + "HT_Taste/Edit", p, function (result) {
                        if (result.Code == 200) {
                            layer.msg(result.Message);
                            $("#myModal").modal("hide");
                            datatable.ajax.reload();
                        }
                        else {
                            layer.msg("更新失败");
                        }
                    })
                })
            })

        }
    </script>
}

